package com.xly.webclick.cusview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.AppCompatDrawableManager;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

import com.xly.webclick.R;


/**
 * Created by amos on 2017/10/12.
 */

public class DuetinCompatImageView extends AppCompatImageView {

    private ColorStateList list;

    public DuetinCompatImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DuetinCompatImageView);
        if (typedArray != null) {

            int drawableRes = typedArray.getResourceId(R.styleable.DuetinCompatImageView_imgSrc, 0);

            int color = typedArray.getColor(R.styleable.DuetinCompatImageView_imgTint, 0);

//            ColorStateList colorStateList = null;
            if (color == 0) {
                list = typedArray.getColorStateList(R.styleable.DuetinCompatImageView_imgTint);
            }
            else {
                list = ColorStateList.valueOf(color);
            }

            if (drawableRes != 0) {
                Drawable drawable = getCompatDrawable(context, drawableRes);

                if (drawable != null) {
                    if (list != null) {
                        drawable = tintDrawable(drawable,list);
                    }
                    setImageDrawable(drawable);
                }
            }

            typedArray.recycle();
        }
    }

    private Drawable getCompatDrawable(Context context, @DrawableRes int id) {
        @SuppressLint("RestrictedApi") Drawable somevectordrable = AppCompatDrawableManager.get().getDrawable(context, id);
        somevectordrable.mutate();
        return somevectordrable;
    }

    private Drawable tintDrawable(Drawable drawable, ColorStateList colorStateList) {
        Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(wrappedDrawable, colorStateList);
        return wrappedDrawable;
    }

    @BindingAdapter({"imgTint"})
    public static void setTintColor(DuetinCompatImageView imageView, int color) {
        Drawable drawable = imageView.getDrawable();
        imageView.list = ColorStateList.valueOf(color);
        if (drawable != null) {
            if (imageView.list != null) {
                imageView.tintDrawable(drawable,imageView.list);
            }
        }
    }

    @BindingAdapter({"imgSrc"})
    public static void setDrawable(DuetinCompatImageView imageView, int resId) {
        Drawable drawable = imageView.getCompatDrawable(imageView.getContext(), resId);
        if (drawable != null) {
            Drawable tintDrawable;
            if (imageView.list != null) {
                tintDrawable = imageView.tintDrawable(drawable,imageView.list);
            }
            else {
                tintDrawable = imageView.tintDrawable(drawable, ColorStateList.valueOf(0));
            }
            imageView.setImageDrawable(tintDrawable);
        }
    }
}
